package com.btt.pattern.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * created by Btt on 2018/07/03
 */

public class CglibMeiPo implements MethodInterceptor{

    public Object getInstance(Class<?> c) throws Exception{

        Enhancer enhancer = new Enhancer();

        //要把哪个设置为即将生成的新类父类
        enhancer.setSuperclass(c);

        enhancer.setCallback(this);

        return enhancer.create();

    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        System.out.println("我是媒婆：我要给你找对象，现在已经拿到你的需求");
        System.out.println("开始物色");

        methodProxy.invokeSuper(o, objects);

        System.out.println("见面，谈恋爱");
        return null;
    }
}
